package ru.amse.stroganova.ui.action;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;

import ru.amse.stroganova.ui.GraphComponent;
import ru.amse.stroganova.ui.command.Command;

/**
 * Presents a factory for graph component convertion actions.
 * 
 * @author Dasha Stroganova
 * 
 */

//TODO all actions here just makes commans (very similar commands). May be it can be refactored.
public class ConvertionActionsFactory {

	private final GraphComponent component;
	
	private final Action toWeightedAction;
	
	private final Action toUnweightedAction;
	
	private final Action toUndirectedAction;
	
	/**
	 * Creates new convertion actions factory for given graph component.
	 * 
	 * @param component
	 */
	public ConvertionActionsFactory(GraphComponent component) {
		this.component = component;
		toWeightedAction = new ToWeightedAction();
		toUnweightedAction = new ToUnweightedAction();
		toUndirectedAction = new ToUndirectedAction();
	}
	
	/**
	 * Returns an action for converting graph from unweighted to weighted.
	 * 
	 * @return action
	 */
	public Action getToWeightedAction() {
		return toWeightedAction;
	}
	
	/**
	 * Returns an action for converting graph from weighted to unweighted.
	 * 
	 * @return action
	 */
	public Action getToUnweightedAction() {
		return toUnweightedAction;
	}
	
	/**
	 * Returns an action for converting graph from directed to undirected.
	 * 
	 * @return action
	 */
	public Action getToUndirectedAction() {
		return toUndirectedAction;
	}
	
	private class ToWeightedAction  extends AbstractAction {
		
		/* package */ToWeightedAction() {
			super("To weighted");
			setEnabled(false);
		}

		public void actionPerformed(ActionEvent e) {
			Command command = component.getCommandFactory().getConvertToWeightedCommand(component.getPresentationController());
			component.addCommand(command);
			command.execute();
		}
	}
	
	private class ToUnweightedAction  extends AbstractAction {
		
		/* package */ToUnweightedAction() {
			super("To unweighted");
			setEnabled(false);
		}

		public void actionPerformed(ActionEvent e) {
			Command command = component.getCommandFactory().getConvertToUnweightedCommand(component.getPresentationController());
			component.addCommand(command);
			command.execute();
		}
	}

	private class ToUndirectedAction  extends AbstractAction {
	
		/* package */ToUndirectedAction() {
			super("To undirected");
			setEnabled(false);
		}

		public void actionPerformed(ActionEvent e) {
			Command command = component.getCommandFactory().getConvertToUndirectedCommand(component.getPresentationController());
			component.addCommand(command);
			command.execute();
		}
	}
}
